package com.thingsgrid.appmonitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thingsgrid.appmanager.entity.FeignVo;
import com.thingsgrid.appmonitor.entity.DeviceInFo;
import com.thingsgrid.appmonitor.service.DeviceService;
import com.thingsgrid.device.entity.Device;
import com.thingsgrid.device.feign.TopmFeign;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 设备状态监控控制类
 */
@RestController
@RequestMapping("appm")
@AllArgsConstructor
public class DeviceController {
	private DeviceService deviceService;
	private TopmFeign topmFeign;

	/**
	 * 接口调用
	 * 分页查询
	 */
	@GetMapping("/deviceDetail")
	@ApiOperation(value = "分页查询", notes = "传入deviceName", position = 2)
	public R<Map<String, Object>> deviceDetail(Page page, String deviceName) {
		ObjectMapper objectMapper = new ObjectMapper();
		List<Device> device_list = new LinkedList<>();//Device属性的list集合
		FeignVo feignVo = new FeignVo();//实例化工具类
		feignVo.setCurrent((int) page.getCurrent());
		feignVo.setSize((int) page.getSize());
		feignVo.setDeviceName(deviceName);
		Object obj_total = topmFeign.GetSomeDeviceInfo(feignVo).getData().get("total");
		Object obj_dev = topmFeign.GetSomeDeviceInfo(feignVo).getData().get("device");
		int total = objectMapper.convertValue(obj_total, Integer.class);
		device_list = objectMapper.convertValue(obj_dev, new TypeReference<List<Device>>() {
		});
		List<DeviceInFo> list = new ArrayList<>();
		for (Device device : device_list) {
			DeviceInFo deviceInFo = deviceService.getDeviceInfoByDeviceId(device);
			list.add(deviceInFo);
		}
		Map<String, Object> returnmap = new HashMap<>();
		returnmap.put("total", total);
		returnmap.put("deviceInfo", list);
		return R.data(returnmap);
	}

}
